package com.labot.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import com.labot.Constants;

public class FileUtils {

	/**
	 * Devuelve un listado de Archivos recursivamente
	 * @param path
	 * @return
	 */
	public static List<File> getFiles(String path) {
		List<File> allFiles = new ArrayList<File>();
		Queue<File> dirs = new LinkedList<File>();
		dirs.add(new File(path));
		while (!dirs.isEmpty()) {
			for (File f : dirs.poll().listFiles()) {
				if (f.isDirectory()) {
					dirs.add(f);
				} else if (f.isFile()) {
					allFiles.add(f);
				}
			}
		}
		return allFiles;
	}
	
	/**
	 * Devuelve un String a partir de un File
	 * @param file
	 * @return
	 * @throws IOException
	 */
	public static String readFile(File file) throws IOException {
		BufferedReader reader = new BufferedReader(new FileReader(file));
		String line = null;
		StringBuilder stringBuilder = new StringBuilder();
		String ls = Constants.FILE_SEP;

		while ((line = reader.readLine()) != null) {
			stringBuilder.append(line);
			stringBuilder.append(ls);
		}
		reader.close();
		return stringBuilder.toString();
	}
	
}
